package customer

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/customer"
    customerReq "counter-help/admin/server/model/customer/request"
)

type CusFeedbackService struct {}

// CreateCusFeedback 创建cusFeedback表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusFeedbackService *CusFeedbackService) CreateCusFeedback(cusFeedback *customer.CusFeedback) (err error) {
	err = global.GVA_DB.Create(cusFeedback).Error
	return err
}

// DeleteCusFeedback 删除cusFeedback表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusFeedbackService *CusFeedbackService)DeleteCusFeedback(ID string) (err error) {
	err = global.GVA_DB.Delete(&customer.CusFeedback{},"id = ?",ID).Error
	return err
}

// DeleteCusFeedbackByIds 批量删除cusFeedback表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusFeedbackService *CusFeedbackService)DeleteCusFeedbackByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]customer.CusFeedback{},"id in ?",IDs).Error
	return err
}

// UpdateCusFeedback 更新cusFeedback表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusFeedbackService *CusFeedbackService)UpdateCusFeedback(cusFeedback customer.CusFeedback) (err error) {
	err = global.GVA_DB.Model(&customer.CusFeedback{}).Where("id = ?",cusFeedback.ID).Updates(&cusFeedback).Error
	return err
}

// GetCusFeedback 根据ID获取cusFeedback表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusFeedbackService *CusFeedbackService)GetCusFeedback(ID string) (cusFeedback customer.CusFeedback, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&cusFeedback).Error
	return
}

// GetCusFeedbackInfoList 分页获取cusFeedback表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusFeedbackService *CusFeedbackService)GetCusFeedbackInfoList(info customerReq.CusFeedbackSearch) (list []customer.CusFeedback, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&customer.CusFeedback{})
    var cusFeedbacks []customer.CusFeedback
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }
	
	err = db.Find(&cusFeedbacks).Error
	return  cusFeedbacks, total, err
}